package com.proyecto.track.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.proyecto.track.bo.Project;
import com.proyecto.track.bo.Rol;
import com.proyecto.track.bo.Task;
import com.proyecto.track.bo.UserPerson;

import com.proyecto.track.service.ProjectService;
import com.proyecto.track.service.TaskService;
import com.proyecto.track.service.RolService;
import com.proyecto.track.service.UserPersonService;

public class ListAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private ProjectService projectService;
	@Autowired
	private TaskService taskService;
	private RolService rolService;
	private UserPersonService userPersonService;
	private Project project = new Project();
	private List<Project> projects = new ArrayList<Project>();
	private List<Task> tasks = new ArrayList<Task>();
	private List<Rol> rols = new ArrayList<Rol>();
	private List<UserPerson> userPersons = new ArrayList<UserPerson>();
	private List<UserPerson> userPersonsOutsideTask = new ArrayList<UserPerson>();
	
	private long id;
	private long idProject;

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public UserPersonService getUserPersonService() {
		return userPersonService;
	}

	public void setUserPersonService(UserPersonService userPersonService) {
		this.userPersonService = userPersonService;
	}

	public List<UserPerson> getUserPersons() {
		return userPersons;
	}

	public void setUserPersons(List<UserPerson> userPersons) {
		this.userPersons = userPersons;
	}

	public RolService getRolService() {
		return rolService;
	}

	public void setRolService(RolService rolService) {
		this.rolService = rolService;
	}

	public List<Rol> getRols() {
		return rols;
	}

	public void setRols(List<Rol> rols) {
		this.rols = rols;
	}

	public List<Task> getTasks() {
		return tasks;
	}

	public void setTasks(List<Task> tasks) {
		this.tasks = tasks;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public ProjectService getProjectService() {
		return projectService;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	public List<Project> getProjects() {
		return projects;
	}

	public void setProjects(List<Project> projects) {
		this.projects = projects;
	}
	
	public List<UserPerson> getUserPersonsOutsideTask() {
		return userPersonsOutsideTask;
	}

	public void setUserPersonsOutsideTask(List<UserPerson> userPersonsOutsideTask) {
		this.userPersonsOutsideTask = userPersonsOutsideTask;
	}
	
	public long getIdProject() {
		return idProject;
	}

	public void setIdProject(long idProject) {
		this.idProject = idProject;
	}

	public String searchProjects(){
		projects = projectService.searchProjects();
		return "SUCCESS";
	}

	public String searchProjectsByAccountId(){
		Map<String,Object> session = ActionContext.getContext().getSession();
		Long accountId = (Long) session.get("accountId");
		projects = projectService.searchProjectsByAccountId(accountId);
		return "SUCCESS";
	}
	
	public String searchTasksProject(){
		tasks = projectService.searchProjects(project.getId()).getTasks();
		return "SUCCESS";
	}
	

	public String listTasksToDo(){
		Map<String,Object> session = ActionContext.getContext().getSession();
		Long accountId = (Long) session.get("accountId");
		tasks = taskService.listTasksToDo(accountId);
		for (Task t : tasks) {
			System.out.println(t.getId());
		}
		
		return "SUCCESS";
	}
	
	public String loadMyTask(){
		Map<String,Object> session = ActionContext.getContext().getSession();
		Long accountId = (Long) session.get("accountId");
		tasks = taskService.listTasksByUser(accountId);
		return "SUCCESS";
	}
	
	
	public String listRolsAndUsers(){
		this.rols = rolService.listRols();
		this.userPersons = userPersonService.searchUser();
		return SUCCESS;
	}
	
	public String loadUsersTask(){
		
		userPersons = taskService.getUsers(id);
		idProject = taskService.searchProjectId(id);
		userPersonsOutsideTask = projectService.getUsers(idProject);
		userPersonsOutsideTask.removeAll(userPersons);
		
		return SUCCESS;
	}
	
	
}
